package io.netty.test.main.chat;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * Created by LuoYu on 2019/2/8.
 */
public class SocketClient {
    public static void main(String[] args) {
        EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
        try {
            Bootstrap bootstrap = new Bootstrap();
            bootstrap.group(eventLoopGroup)
                .channel(NioSocketChannel.class) //区别于服务端的连接NioServerSocketChannel
                .handler(new ClientInitializer());
            ChannelFuture future = bootstrap.connect("localhost", 6666).sync();
            // 读取标准输入
            BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
            Channel channel = future.channel();
            for(;;){
                channel.writeAndFlush(reader.readLine() + "\r\n");
            }
        } catch (InterruptedException | IOException e) {
            e.printStackTrace();
        } finally {
            eventLoopGroup.shutdownGracefully();

        }
    }
}
